1. Jupyter Notebooks

Jupyter notebooks are an interface to a programming language, surrounded by a rich environment for documenting, visualizing, and writing.

1.1 Backends

When you open or create a notebook, you are actually starting two things:

  1. a backend kernel process that will handle the execution of your code
  2. a frontend webpage that allows typing in code, saving the notebook, etc.

The backend kernel can be in any number of programming languages, including Python 2, Python 3, or Scheme. Initially, the backend will start in Python 2, but you can change that after you create the notebook. After you create a new notebook, you will be in the notebook editor frontend (see below). In the upper right-hand corner, simply change the pull-down list from IPython (Python 2) to Scheme.

This server is running on a Linux computer. You can also talk directly to this computer by using "shell magics" discussed below.

1.2 Frontends

The Jupyter Project at Bryn Mawr College will use the web interface. This allows you to edit, document, and execute code in a web browser.

1.2.1 Code

Using Jupyter, you'll be able to execute code in many computer languages. By default, a newly created notebook will use "IPython (Python 2)". You can also install other computer languages. Once you change a notebook's associated language, it will remember your choice, and attempt to startup that language.

Code is executed by a "kernel". We will refer to a language as a kernel in much of the Jupyter documentation.

1.2.2 Magics

Jupyter uses the word "magic" to refer to "meta commands". These meta commands are largely independent of the particular kernel you are using. However, the magics for each kernel may differ.

Below we show some command magics.

1.2.2.1 Shell commands

In a Jupyter notebook, you can talk directly to the computer through an operating system shell. To list out the files in the current directy, us the ls command:

In [2]:
! ls -l
total 188
-rw-r--r-- 1 dblank dblank  3111 Aug 31 14:51 00 Jupyter Notebooks.ipynb
-rw-r--r-- 1 dblank dblank 18385 Aug 27 06:20 01 Scheme, Introduction.ipynb
drwxrwxr-x 2 dblank dblank  4096 Aug 29 12:54 images
-rw-r--r-- 1 dblank dblank 17195 Aug 30 10:13 Jupyter Help.ipynb
-rw-r--r-- 1 dblank dblank  3015 Aug 29 15:57 Jupyter Magics.ipynb
-rw-r--r-- 1 dblank dblank 92667 Aug 29 13:02 Jupyter Notebook Users Manual.ipynb
-rw-r--r-- 1 dblank dblank 39037 Aug 29 20:42 Jupyter Visualizations.ipynb
-rw-r--r-- 1 dblank dblank  2960 Aug 29 20:36 timeline.html

Use pwd to "print working direct":

In [3]:
! pwd
/home/dblank/Public

1.2.2.2 Help commands

You can usually put a question mark before or after an item's name and execute the shell. Usually, one question mark means "give me a hint about what this is"; two question marks mean "give me more details". You can also press SHIFT+TAB after a word in an expression and see the hint. In these examples, we are using the Calico Scheme kernel:

In [1]:
?car
In [2]:
??car

Press SHIFT+TAB with the cursor after the last e in "define":

In [ ]:
(define ...)

1.2.2.3 Magic commands

Magic commands start out with either a "!", "?", or "%":

In [4]:
%magic
Line magics:
    %cd PATH - change current directory of session
    %connect_info - show connection information
    %download URL [FILENAME] - download file from URL
    %html CODE - display code as HTML
    %install_magic URL - download and install magic from URL
    %javascript CODE - send code as JavaScript
    %latex TEXT - display text as LaTeX
    %lsmagic - list the current line and cell magics
    %magic - show installed magics
    %plot [options] backend - configure plotting for the session.
    %reload_magics - reload the magics from the installed files
    %shell COMMAND - run the line as a shell command

Cell magics:
    %%file [--append|-a] FILENAME - write contents of cell to file
    %%html - display contents of cell as HTML
    %%javascript - send contents of cell as JavaScript
    %%latex - display contents of cell as LaTeX
    %%shell - run the contents of the cell as shell commands
    %%time - show time to run cell

Shell shortcut:
    ! COMMAND ... - execute command in shell

Any cell magic can be made persistent for rest of session by using %%% prefix.

Help on items:
    ??item - get detailed help on item
    ?item - get help on item

1.3 Documenting code

Please see Jupyter Notebook Users Manual for more information on editing and documenting a notebook.